import pygame

class Block(pygame.Rect):

    def __init__(self, x, y, width, height, color, **kwargs):
        # super().__init__(x, y, width, height)
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        if kwargs is not None and len(kwargs) > 0:
            # 动态设置额外的属性
            for key, value in kwargs.items():
                setattr(self, key, value)
        else:
            self.color_text = (0,0,0)
            self.text_surface = None
            self.font = None
            self.text = ""
            self.image = None
            self.is_x_left = False
            self.is_y_donw = False
            self.border = 0
            self.border_color = (0,0,0)
            self.zhan = 0
            self.scour = 0
            self.time = 0;

    def __copy__(self):
        # 创建一个字典的副本，以避免修改原始字典
        attrs = self.__dict__.copy()
        # 移除那些在构造函数中作为位置参数传递的键
        for attr in ['x', 'y', 'width', 'height', 'color']:
            attrs.pop(attr, None)
        # 使用构造函数的位置参数和剩余的键值对来创建新的Block实例
        return Block(self.x, self.y, self.width, self.height, self.color, **attrs)
    def move(self, dx, dy):
        self.x += dx
        self.y += dy

    def setText(self,pygame,text):
        # 设置字体、大小
        self.text = text
        self.font = pygame.font.Font(None, 50)
        self.text_surface = self.font.render(text, True, self.color_text)
        # setattr(self,"text_surface",text_surface)
    def setImage(self,block_img):
        self.image = block_img

    # 返回自身坐标
    def show(self):
        return (self.x,self.y,self.width,self.height)

    def size(self):
        return (self.width,self.height)

    def draw(self, screen,pygame):
        rect = pygame.Rect(self.show())
        if self.image is not None:
            screen.blit(self.image,(self.x,self.y))
        else:
            pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))
            if self.border > 0:
                pygame.draw.rect(screen, self.border_color, (self.x, self.y, self.width, self.height),self.border)
        if self.text_surface is not None:
            self.text_surface = self.font.render(self.text, True, self.color_text)  # 重新渲染文本
            self.text_surface_rect = self.text_surface.get_rect()  # 获取新的矩形
            self.text_surface_rect.center = rect.center  # 更新中心点
            screen.blit(self.text_surface, self.text_surface_rect)  # 绘制文本



